package org.xncj.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.entity.PageResult;
import org.entity.QueryPageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xncj.CheckItem;
import org.xncj.SetMealService;
import org.xncj.Setmeal;
import org.xncj.dao.SetMealDao;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Resource
    SetMealDao setMealDao;

    @Override
    public void addSetMeal(Integer[] checkGroupIds, Setmeal setmeal) {
        setMealDao.addSetMeal(setmeal);
        Integer setmealId = setmeal.getId();

        HashMap<String ,Integer> map = new HashMap<>();
        map.put("setmealId",setmealId);
        for(Integer checkGroupId : checkGroupIds){
            map.put("checkGroupId",checkGroupId);
            setMealDao.setSetMealAdd(map);
        }
    }

    @Override
    public List<Setmeal> findAll() {
        return setMealDao.findAll();
    }

    @Override
    public PageResult findAllsetmeal(QueryPageBean queryPageBean) {
        if ("null".equals(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("null");
        }
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = setMealDao.findAllsetmeal(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void updatesetmeal(Integer setmealId) {
        setMealDao.updatesetmeal(setmealId);
    }

    @Override
    public void Editsetmeal(Integer[] setmealId, Setmeal setmeal) {
        Integer setmealid = setmeal.getId();
        setMealDao.deletesetmealBycheckGroup(setmealid);
        setMealDao.upEditsetmealid(setmeal);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("setmealid",setmealid);
        for(Integer setmealids : setmealId){
            map.put("setmealids" ,setmealids);
            setMealDao.setCheckgroupAndsetmeal(map);
        }
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return SetMealDao.findSetmealCount();
    }
}
